package com.fy56.platform.logistics.utils;

import com.alibaba.fastjson.JSON;
import com.fy56.platform.logistics.common.ResultModel;
import com.google.common.collect.Maps;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import static org.apache.http.entity.ContentType.APPLICATION_FORM_URLENCODED;

/**
 * @author  by Edwin Yang on 2018/3/21 0021.
 */
public class SmsUtil {


    private static final String SMS_PROPERTIES = "sms.properties";

    /**
     * 发送短信验证码功能;
     * Created by Yangjiyu on 2018-01-29.
     */
    public static Map<String, String> sendValidateCode(Map<String,String> params){
        String SMSClientUrl = IOUtils.getConfigParam("smsValidateURL", SMS_PROPERTIES) + "/smsValidate/sendValidateSms";
        String sendSMSTenant = IOUtils.getConfigParam("smsDefaultTenant", SMS_PROPERTIES);
        String tenantcode     = params.get("code")+"";//金融代码有定制版，无是公版
        String password = params.get("password")+"";//金融密码



        System.out.println(" sendValidateCode:"+params.toString());

        Map<String, String> map = Maps.newHashMap();
        String message = null;
        Boolean success = false;
        try {
            String postResult ="";
            if (StringUtil.isNotEmpty(tenantcode)){
                SMSClientUrl = IOUtils.getConfigParam("smsValidateURL", SMS_PROPERTIES) + "/smsValidate/sendValidateNewSms";
                Map<String, String> paramsNew = Maps.newHashMap();
                paramsNew.put("tenant",tenantcode);
                paramsNew.put("mobileNo",params.get("mobileNo")+"");
                paramsNew.put("sendType",params.get("sendType")+"");
                paramsNew.put("passWord",password);
                postResult = HttpClientUtils.post(SMSClientUrl,paramsNew,setHeader(),20000,20000);
            }else{
                params.put("tenant",sendSMSTenant);
                postResult = HttpClientUtils.post(SMSClientUrl,params,setHeader(),20000,20000);
            }

            if (postResult != null) {
                int code = Integer.valueOf(JSON.parseObject(postResult).get("code").toString());
                if (ResultModel.ok().getCode() == code) {
                    message = JSON.parseObject(postResult).get("content").toString();
                    success = true;
                } else {
                    message = "发送失败:" + JSON.parseObject(postResult).get("content").toString();
                }
            } else {
                message = "发送失败";
            }
        }catch (Exception e){
            message = "发送失败:" + "发送短信验证码请求出错";;
        }
        map.put("message", message);
        map.put("success", success.toString());
        return map;
    }

    /**
     * 匹配验证码功能
     * Created by Yangjiyu on 2018-01-29.
     */
    public static Map<String, String> checkValidateCode(Map<String,String> params){
        String SMSClientUrl = IOUtils.getConfigParam("smsValidateURL", SMS_PROPERTIES) + "/smsValidate/validateByCode";
        Map<String, String> map = Maps.newHashMap();
        String message = null;
        Boolean success = false;
        try {
            String postResult = HttpClientUtils.post(SMSClientUrl,params,setHeader(),20000,20000);

            if (postResult != null) {
                int code = Integer.valueOf(JSON.parseObject(postResult).get("code").toString());
                if (ResultModel.ok().getCode() == code) {
                    if("success".equals(JSON.parseObject(postResult).get("content").toString())){
                        message = "验证成功";
                        success = true;
                    }else if("error".equals(JSON.parseObject(postResult).get("content").toString())){
                        message = "验证失败,验证码错误";
                        success = false;
                    }else{
                        message = "验证失败,验证码失效,请重新发送";
                        success = false;
                    }
                } else {
                    message = "参数错误,请求验证失败:" + JSON.parseObject(postResult).get("content").toString();
                }
            } else {
                message = "验证失败";
            }
        }catch (Exception e){
            message = "验证失败:" + "POST请求出错";
        }
        map.put("message", message);
        map.put("success", success.toString());
        return map;
    }


    /**
     * 微信推送功能;
     */
    public static String sendWechatPush(Map<String,String> params) throws Exception {
        String wechatPushUrl = IOUtils.getConfigParam("sendURL", SMS_PROPERTIES);
        Map<String, String> postParams = null;
        params.put("sendType","SMS");
        params.put("bizNo", UUID.randomUUID().toString());
        params.put("bizType","1");
        params.put("appPlatform", "BS");
        postParams = Maps.newHashMap();
        postParams.put("data", JSON.toJSONString(params));
        return HttpUtil.post(wechatPushUrl,params);
    }

    public static Map<String, String> setHeader(){
        Map<String, String> header = Maps.newHashMap();
        header.put("Authorization", "ignore ");
        header.put("Content-type", String.valueOf(APPLICATION_FORM_URLENCODED));
        return header;
    }
}
